# -*- coding: utf-8 -*-
# @Author	: starview.guojian.ma
# @Date		: 2022/11/7 10:09
# @Last Modified by:   starview.guojian.ma
# @Last Modified time: 2022/11/7 10:09
# Thanks for your comments!

import json
import logging
import traceback

import aiohttp

logger = logging.getLogger(__name__)


async def send_sync_post_request(server_url, data, headers=None, verify_ssl=None, **kwargs):
    try:
        logger.info(f"request url: {server_url}, headers:{headers}, request_data:{data}")
        async with aiohttp.ClientSession() as session:
            async with session.post(url=server_url, data=data, headers=headers, verify_ssl=verify_ssl, **kwargs) as res:
                response = await res.text()
                logger.info(f"response url:{server_url}, response data: {response}")
                return res.status, json.loads(response)
    except Exception as e:
        logger.error(f"send post request: {server_url} failed with err:{str(e)}")
        return None, str(e)


async def send_sync_get_request(server_url, response_encoding="utf-8", is_print_res=False, verify_ssl=None,
                                headers=None, **kwargs):
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url=server_url, verify_ssl=verify_ssl, headers=headers, **kwargs) as res:
                response = await res.text(encoding=response_encoding)
                if is_print_res is True:
                    logger.info(f"response url:{server_url}, data: {response}")
                return res.status, response
    except Exception as e:
        logger.error(traceback.format_exc())
        logger.error(f"send get request: {server_url} failed with err:{str(e)}")
        return None, str(e)


if __name__ == "__main__":
    # loop = asyncio.get_event_loop()
    url = 'https://api.weixin.qq.com/sns/userinfo?access_token=66_K7nhKNiW83V4o3qKukfa1k4dawirleZvX2Y_B07tUvUyB5124ZUSfZyZNiUgNu_b-VIXTTNPQXV54yzYu7k2UN7zT7F7lx4s2xR3FdoPXqk&openid=oX3W41XHeYPhbzIFJ05Gyf3SSM3Y&ang=zh_CN'
    # loop.run_until_complete(send_sync_get_request(url))
    import requests

    result = requests.get(url)
    print(result)
